package com.cskaoyan.config.timer;

import com.cskaoyan.bean.CouponUser;
import com.cskaoyan.bean.CouponUserExample;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserExample;
import com.cskaoyan.mapper.CouponUserMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.utils.CouponPastInformUtil;
import com.cskaoyan.utils.DateUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;
import java.util.List;

/**
 * Description
 *
 * @author Zhangfei
 * @date Created on 2021/10/25
 */
public class CouponPastInformTask extends QuartzJobBean {
    @Autowired
    CouponUserMapper couponUserMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        //查询出所有即将过期的优惠卷
        CouponUserExample example = new CouponUserExample();
        CouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andEndTimeLessThan(DateUtil.getAddDate(new Date(), 1)).andStatusEqualTo(0);
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(example);
        //如果不为null,则进行以下操作
        if (couponUsers != null && couponUsers.size() > 0) {
            for (CouponUser couponUser : couponUsers) {
                //查询用户name
                UserExample userExample = new UserExample();
                UserExample.Criteria userExampleCriteria = userExample.createCriteria();
                userExampleCriteria.andDeletedEqualTo(false).andIdEqualTo(couponUser.getUserId());
                List<User> users = userMapper.selectByExample(userExample);
                if (users != null && users.size() > 0) {
                    //说明用户存在然后给用户发信息
                    String username = users.get(0).getUsername();
                    //如果邮箱注册则发送邮箱
                    if (CouponPastInformUtil.checkEmail(username)) {

                        CouponPastInformUtil.sendMailRandomCode(username);
                    }
                    // //如果短信注册则发短信
                    // if (CouponPastInformUtil.checkPhone(username)) {
                    //
                    //     return;
                    // }

                    //为了患者体验，不应该一直发，所以，正常逻辑是放到redis当中,每次查询的时候设置时间
                    // Jedis jedis = JedisUtil.getJedis();
                    // jedis.set(username,86400,"123");
                }

            }
        }
    }
}
